//
//  TXUserAccount.swift
//  MicroBlog
//
//  Created by Jokin on 2017/3/29.
//  Copyright © 2017年 jokin. All rights reserved.
//

import UIKit

let UserAccountPath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent("userAccount.archive")



class TXUserAccount: NSObject,NSCoding {
    
//    "access_token": "ACCESS_TOKEN",
//    "expires_in": 1234,
//    "remind_in":"798114",
//    "uid":"12341234"
    
    var access_token : String?
    var expires_in : Int = 0
    var uid : Int64 = 0
    var expirceDate : Date?
    
    var name : String?
    var avatar_large : String?
    
    
    
    init(dic: [String : Any]) {
        super.init()
        setValuesForKeys(dic)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
         
    }
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expires_in, forKey: "expires_in")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(expirceDate, forKey: "expirceDate")
    }
    
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        expires_in = Int(aDecoder.decodeCInt(forKey: "expires_in"))
        uid = aDecoder.decodeInt64(forKey: "uid")
        expirceDate = aDecoder.decodeObject(forKey: "expirceDate") as? Date
        
    }
    
    func saveAcoder() {
        NSKeyedArchiver.archiveRootObject(self, toFile: UserAccountPath)
    }
    
    class func loadUserAccount() ->TXUserAccount? {
        
        let obj = NSKeyedUnarchiver.unarchiveObject(withFile: UserAccountPath)
        return obj as? TXUserAccount
        
    }
    

}
